fix: Use ReturnType for more robust Chainable type#2098
fix: Use ReturnType for more robust Chainable type#2098dprevost-LMI wants to merge 1 commit intowebdriverio:mainfrom
ReturnType for more robust Chainable type#2098Conversation
Greptile SummaryThis PR changes the two local type aliases Confidence Score: 4/5Safe to merge — the change is a targeted, well-motivated type-only fix with no runtime impact. Only P2 findings present. The approach is sound and the PR description clearly explains the motivation. The one concern (ReturnType on overloaded methods) is a latent fragility rather than a present bug. No files require special attention.
|
| Filename | Overview |
|---|---|
| types/expect-webdriverio.d.ts | Two type aliases rewritten to use ReturnType from the global WebdriverIO namespace; logically sound but ReturnType on overloaded methods captures only the last overload, worth keeping in mind. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["Consumer type-checks\nexpect(element)"] --> B{Which type source?}
B -- "Before PR\n(direct import)" --> C["import('webdriverio').ChainablePromiseElement\n(may resolve different version)"]
B -- "After PR\n(ReturnType)" --> D["ReturnType<WebdriverIO.Browser['$']>\n(same namespace as consumer)"]
C --> E["⚠️ Type mismatch —\nextends check fails\n(version conflict)"]
D --> F["✅ Same type universe —\nextends check succeeds"]
Reviews (1): Last reviewed commit: "Use `ReturnType` for more robust Chainab..." | Re-trigger Greptile
| type ChainablePromiseElement = ReturnType<WebdriverIO.Browser['$']> | ||
| type ChainablePromiseArray = ReturnType<WebdriverIO.Browser['$$']> |
There was a problem hiding this comment.
ReturnType on overloaded methods picks only the last overload
TypeScript's ReturnType resolves to the return type of the last overload signature when the target is an overloaded function. WebdriverIO.Browser['$'] and ['$$'] are typically overloaded (accepting string, Function, Element, etc.), so if any overload other than the last carries a different return type the alias would silently diverge from its intended meaning. In practice all overloads of $/$$ currently return ChainablePromiseElement/ChainablePromiseArray respectively, so this is not a bug today — but it's a subtle coupling to the ordering of WebdriverIO's internal overload declarations that could break silently if upstream changes them. A comment documenting this assumption would help future maintainers.
In jest playgrounds,
ChainablePromiseElement&ChainablePromiseArrayhad difficulty resolving properly due to a conflictingwebdriverioversion. UsingReturnType<WebdriverIO.Browser['$']>make it more reliable.Why ReturnType<WebdriverIO.Browser['$']> fixed it: